/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.expandableview.slice;

import com.example.expandableview.ExpandedListItemView;
import com.example.expandableview.ResourceTable;
import com.expandable.view.ExpandableView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;

/**
 * MainAbilitySlice
 *
 * @since 2021-06-07
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    private static final int VISIBLELAYOUTHEIGHT = 100;
    private static final int HOLOBLUELIGHT = 0xff33b5e5;
    private static final int HOLOGREENLIGHT = 0xff99cc00;
    private ExpandableView topExpandableView;
    private ExpandableView middleExpandableView;
    private ExpandableView expandableViewLevel1;
    private ExpandableView expandableViewLevel2;
    private ExpandableView expandableViewLevel3;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initView() {
        topExpandableView = (ExpandableView) findComponentById(ResourceTable.Id_main_top_expandable_view);
        middleExpandableView = (ExpandableView) findComponentById(ResourceTable.Id_main_middle_expandable_view);
        topExpandableView = (ExpandableView) findComponentById(
            ResourceTable.Id_main_top_expandable_view);
        middleExpandableView = (ExpandableView) findComponentById(
            ResourceTable.Id_main_middle_expandable_view);

        expandableViewLevel1 = new ExpandableView(this);
        expandableViewLevel2 = new ExpandableView(this);
        expandableViewLevel3 = new ExpandableView(this);

        expandableViewLevel1.setHeightChangeListener(topExpandableView);
        expandableViewLevel2.setHeightChangeListener(expandableViewLevel1);
        expandableViewLevel3.setHeightChangeListener(expandableViewLevel2);
        createTopExpandableView();
        createMiddleExpandableView();

        createInnerExpandableViewLevel1();
        createInnerExpandableViewLevel2();
        createInnerExpandableViewLevel3();

        expandableViewLevel1.setOutsideContentLayout(topExpandableView.getContentLayout());
        expandableViewLevel2.setOutsideContentLayout(topExpandableView.
            getContentLayout(), expandableViewLevel1.getContentLayout());
        expandableViewLevel3.setOutsideContentLayout(topExpandableView.
            getContentLayout(), expandableViewLevel1.getContentLayout(), expandableViewLevel2.getContentLayout());
    }

    private void createTopExpandableView() {
        try {
            String[] ohosVersionNameList = this.getResourceManager().
                getElement(ResourceTable.Strarray_ohos_version_names).getStringArray();
            topExpandableView.fillData(ResourceTable.Media_icon, getString(ResourceTable.String_ohos_names), true);
            addContentView(topExpandableView, ohosVersionNameList, true);
            topExpandableView.addContentView(expandableViewLevel1);
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(LABEL, e.getMessage());
        }
    }

    private void createMiddleExpandableView() {
        try {
            String[] ohosVersionNameList = this.getResourceManager().
                getElement(ResourceTable.Strarray_ohos_version_names).getStringArray();
            middleExpandableView.fillData(ResourceTable.Media_icon, getString(ResourceTable.String_ohos_names), false);
            middleExpandableView.setVisibleLayoutHeight(VISIBLELAYOUTHEIGHT);
            addContentView(middleExpandableView, ohosVersionNameList, false);
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(LABEL, e.getMessage());
        }
    }

    private void createInnerExpandableViewLevel1() {
        try {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(HOLOBLUELIGHT));
            expandableViewLevel1.setBackground(element);
            expandableViewLevel1.fillData(ResourceTable.Media_icon, getString(ResourceTable.String_ohos_codes), false);
            String[] ohosVersionNumberList = this.getResourceManager().
                getElement(ResourceTable.Strarray_ohos_version_time).getStringArray();
            addContentView(expandableViewLevel1, ohosVersionNumberList, false);
            expandableViewLevel1.addContentView(expandableViewLevel2);
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(LABEL, e.getMessage());
        }
    }

    private void createInnerExpandableViewLevel2() {
        try {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(HOLOGREENLIGHT));
            expandableViewLevel2.setBackground(element);
            expandableViewLevel2.fillData(ResourceTable.Media_icon, getString(ResourceTable.String_ohos_names), false);
            String[] ohosVersionNameList = this.getResourceManager().
                getElement(ResourceTable.Strarray_ohos_version_names).getStringArray();
            addContentView(expandableViewLevel2, ohosVersionNameList, false);
            expandableViewLevel2.addContentView(expandableViewLevel3);
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(LABEL, e.getMessage());
        }
    }

    private void createInnerExpandableViewLevel3() {
        try {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(HOLOBLUELIGHT));
            expandableViewLevel3.setBackground(element);
            expandableViewLevel3.fillData(ResourceTable.Media_icon, getString(ResourceTable.String_ohos_codes), false);
            String[] ohosVersionNameList = this.getResourceManager().
                getElement(ResourceTable.Strarray_ohos_version_time).getStringArray();
            addContentView(expandableViewLevel3, ohosVersionNameList, false);
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(LABEL, e.getMessage());
        }
    }

    private void addContentView(ExpandableView view, String[] stringList, boolean isShowCheckbox) {
        for (String ss : stringList) {
            ExpandedListItemView itemView = new ExpandedListItemView(this);
            itemView.setText(ss, isShowCheckbox);
            view.addContentView(itemView);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
